package soccer.view;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;

public class StatisticPanel extends JPanel {
	private JTable leagueTable;
	private JButton teamStatisticButton;
	private JButton playerStatisticButton;
	private JLabel weekLabel;
	private JComboBox weekComboBox;

	/**
	 * Create the panel.
	 */
	public StatisticPanel() {
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(69, 45, 430, 189);
		add(scrollPane);
		
		leagueTable = new JTable();
		leagueTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null},
			},
			new String[] {
				"Team Name", "Played Match", "Won", "Defeated", "Draw", "Average", "Point"
			}
		));
		leagueTable.getColumnModel().getColumn(0).setPreferredWidth(105);
		leagueTable.getColumnModel().getColumn(2).setPreferredWidth(38);
		leagueTable.getColumnModel().getColumn(3).setPreferredWidth(63);
		leagueTable.getColumnModel().getColumn(4).setPreferredWidth(46);
		leagueTable.getColumnModel().getColumn(5).setPreferredWidth(54);
		leagueTable.getColumnModel().getColumn(6).setPreferredWidth(42);
		scrollPane.setViewportView(leagueTable);
		
		JLabel lblNewLabel = new JLabel("Soccer League Table");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(82, 20, 372, 14);
		add(lblNewLabel);
		
		weekComboBox = new JComboBox();
		weekComboBox.setBounds(10, 60, 49, 20);
		add(weekComboBox);
		
		weekLabel = new JLabel("Week :");
		weekLabel.setBounds(10, 46, 46, 14);
		add(weekLabel);
		
		playerStatisticButton = new JButton("Player Statistic");
		playerStatisticButton.setBounds(130, 252, 111, 23);
		add(playerStatisticButton);
		
		teamStatisticButton = new JButton("Team Statisic");
		teamStatisticButton.setBounds(301, 252, 120, 23);
		add(teamStatisticButton);

	}
	public void fillComboBoxWithWeek(int week){
	    Object[] items = new Object[week];
		for(int i = 0; i < week; i++){
	    	items[i] = i+1;
	    }
		DefaultComboBoxModel model = new DefaultComboBoxModel(items);
		weekComboBox.setModel(model);
		weekComboBox.setSelectedIndex(week-1);
	}
	public  Object getSelectedTeamFromComboBox(){
		Object selectedItem = weekComboBox.getSelectedItem();
		return selectedItem;
	}
	public void addLeagueTable(List leagueTableList){
		if(leagueTableList == null || leagueTableList.size() < 1){
		
		 }
		DefaultTableModel tableModel = (DefaultTableModel) leagueTable.getModel();
        resetTeamTable(tableModel);
		for(int i = leagueTableList.size()-1; i >= 0 ; i--){
			Map league = (Map) leagueTableList.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) league.get("teamName"));
			v.add((Comparable) league.get("playedMatchNumber"));
			v.add((Comparable) league.get("wonMatchNumber"));
			v.add((Comparable) league.get("drawMatchNumber"));
			v.add((Comparable) league.get("defeatedMatchNumber"));
			v.add((Comparable) league.get("average"));
			v.add((Comparable) league.get("point1"));
			tableModel.addRow(v);
		}
	 //searchPlayerResultInfoLabel.setVisible(false);
     //playerTablesSrollPane.setVisible(true);
	}
	public void resetTeamTable(DefaultTableModel model){
		while(model.getRowCount() > 0){
			model.removeRow(0);
		}
	}
	public void addWeekComboboxItemListener(ItemListener l){
		 weekComboBox.addItemListener(l);
	}
	public void addPlayerStatisticButtonActionListener(ActionListener l){
    	playerStatisticButton.addActionListener(l);
    }
	public void addTeamStatisticButtonActionListener(ActionListener l){
    	teamStatisticButton.addActionListener(l);
    }
	/*public void selectBackToUserMenu(ActionListener l){
		backUserOptionButton.addActionListener(l);
	}*/
	
	
	public JTable getLeagueTable() {
		return leagueTable;
	}

	public void setLeagueTable(JTable leagueTable) {
		this.leagueTable = leagueTable;
	}

	public JButton getTeamStatisticButton() {
		return teamStatisticButton;
	}

	public void setTeamStatisticButton(JButton teamStatisticButton) {
		this.teamStatisticButton = teamStatisticButton;
	}

	public JButton getPlayerStatisticButton() {
		return playerStatisticButton;
	}

	public void setPlayerStatisticButton(JButton playerStatisticButton) {
		this.playerStatisticButton = playerStatisticButton;
	}

	public JLabel getWeekLabel() {
		return weekLabel;
	}

	public void setWeekLabel(JLabel weekLabel) {
		this.weekLabel = weekLabel;
	}

	public JComboBox getWeekComboBox() {
		return weekComboBox;
	}

	public void setWeekComboBox(JComboBox weekComboBox) {
		this.weekComboBox = weekComboBox;
	}
	   
}
